package com.nanyang.tms.service.ticket.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.ticket.*;
import com.nanyang.tms.api.dto.response.ticket.TicketPageResponse;
import com.nanyang.tms.api.dto.response.ticket.TicketRecordResponse;
import com.nanyang.tms.api.dto.response.ticket.TicketResponse;
import com.nanyang.tms.api.service.ticket.TicketRpcService;
import com.nanyang.tms.dao.ticket.TicketTypeDao;
import com.nanyang.tms.domain.ticket.*;
import com.nanyang.tms.manager.ticket.TicketManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class TicketRpcServiceImpl implements TicketRpcService {

    @Autowired
    private TicketManager ticketManager;
    @Autowired
    private TicketTypeDao ticketTypeDao;
    @Override
    public ApiResult<PageBean<TicketPageResponse>> listTicketType(TicketQueryRequest param) {
        ApiResult<PageBean<TicketPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            TicketTypeQuery query = new TicketTypeQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<TicketTypeVO> ticketList = ticketManager.listTicket(query);
            PageBean<TicketPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(ticketList.getTotalItem());
            BeanUtils.copyListProperties(ticketList.getData(), resultList.getData(), TicketPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<TicketResponse> selectByCode(String code) {
        ApiResult<TicketResponse> result = new ApiResult<>();
        try {
            TicketResponse  ticketResponse = new TicketResponse();
            TicketTypeVO ticketTypeVO = ticketTypeDao.selectByCode(code);
            if(Objects.isNull(ticketTypeVO)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(ticketTypeVO,  ticketResponse);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( ticketResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveTicketType(TicketAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            TicketTypeDO ticketTypeDO = new  TicketTypeDO();
            BeanUtils.copyProperties(param,ticketTypeDO);
            ticketTypeDO.setGmtCreatePin(param.getPin());
            ticketManager.saveTicketType(ticketTypeDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateTicketType(TicketUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            TicketTypeDO ticketTypeDO = new  TicketTypeDO();
            BeanUtils.copyProperties(param,ticketTypeDO);
            ticketTypeDO.setGmtModifiedPin(param.getPin());
            ticketManager.updateTicketType(ticketTypeDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.TICKET,template="ticketDel")
    @Override
    public ApiResult<Boolean> delTicketType(String ticketTypeCode,String pin) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            ticketManager.delTicketType(ticketTypeCode);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public  ApiResult<PageBean<TicketRecordResponse>> listTicketRecord(TicketRecordQueryRequest param){
        ApiResult<PageBean<TicketRecordResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            TicketRecordQuery query = new TicketRecordQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<TicketRecordVO> ticketList = ticketManager.listTicketRecord(query);
            PageBean<TicketRecordResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(ticketList.getTotalItem());
            BeanUtils.copyListProperties(ticketList.getData(), resultList.getData(), TicketRecordResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<Boolean> importTicketNo(TicketImportRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            TicketImportVO ticketImportVO=new TicketImportVO();
            BeanUtils.copyProperties(param,ticketImportVO);
            ticketImportVO.setGmtCreatePin(param.getPin());
            ticketManager.importTicketNo(ticketImportVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
